package com.yanqu.road.server.cmd.activity.wishpool;

import com.yanqu.road.pb.activity.WishPoolProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.wishpool.WishPoolModule;
import com.yanqu.road.server.manger.activity.wishpool.WishPoolMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = GameProtocol.S_ACTIVITY_WISH_POOL_WISH_ACTION, desc = "许愿操作")
public class WishActionCmd extends AbstractCommand {

    private String reward = "";

    private boolean isBigReward = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!WishPoolMgr.isActivityInTime()) {
            ret = GameErrorCode.E_ACTIVITY_WISH_POOL_NOT_IN_TIME;
            return;
        }

        WishPoolModule wishPoolModule = player.getModule(WishPoolModule.class);
        if (!wishPoolModule.isRewardSet()) {
            ret = GameErrorCode.E_ACTIVITY_WISH_POOL_SHOULD_SET_REWARD;
            return;
        }

        // list[0]：结果，0次数不足，1普通奖励，2大奖励；list[1]：获得的奖励
        List<String> result = wishPoolModule.doWish();
        if (StringUtils.stringEquals(result.get(0), "0")) {
            ret = GameErrorCode.E_ACTIVITY_WISH_POOL_WISH_TIMES_NO_ENOUGH;
            return;
        }
        isBigReward = StringUtils.stringEquals(result.get(0), "2");
        reward = result.get(1);
    }

    @Override
    public void sendData(GamePlayer player) {
        WishPoolProto.WishActionRespMsg.Builder builder = WishPoolProto.WishActionRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setReward(reward);
            builder.setIsBigReward(isBigReward);
        }
        player.sendPacket(ClientProtocol.U_ACTIVITY_WISH_POOL_WISH_ACTION, builder);
    }
}
